home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1984-1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
-
- #include <gl.h>
- #include <stdio.h>
- #include <fcntl.h>
-
- #include "collision.h"
-
-
- /*
- * collide_tri() returns true if the given point has a y value less than the
- * y value of the given triangle at the given x and z location.
- *
- * A-------B
- * | P /
- * | /
- * | /
- * |/
- * C
- */
- int collide_tri(px, py, pz, ax, bx, az, cz, aelv, belv, celv)
- float px, py, pz, ax, bx, az, cz, aelv, belv, celv;
- {
- float delta1, elv1, delta2, elv2;
-
- delta1 = (ax - px) / (ax - bx);
- elv1 = aelv - ((aelv - belv) * delta1);
- delta2 = (az - pz) / (az - cz);
- elv2 = elv1 - ((aelv - celv) * delta2);
-
- if (py < elv2)
- return(TRUE);
- else
- return(FALSE);
- }
-
-
-
- /*
- * collide_grid() returns true if the given point is lower than the given grid
- * at the point's x and z location.
- */
- int collide_grid(float px, float py, float pz, grid_t *g)
- {
- int gx, gz;
- float rpx, rpz;
-
- if (px > g->xmin && px < g->xmax &&
- pz > g->zmin && pz < g->zmax)
- {
- gx = (px - g->xmin) / g->stepsize;
- gz = (pz - g->zmin) / g->stepsize;
- rpx = px - gx * g->stepsize - g->xmin;
- rpz = pz - gz * g->stepsize - g->zmin;
- if (rpx + rpz < g->stepsize)
- return(collide_tri(rpx, py, rpz, 0.0, g->stepsize, 0.0, g->stepsize,
- g->elv[gx][gz], g->elv[gx+1][gz], g->elv[gx][gz+1]));
- else
- return(collide_tri(rpx, py, rpz, g->stepsize, 0.0, g->stepsize, 0.0,
- g->elv[gx+1][gz+1], g->elv[gx][gz+1], g->elv[gx+1][gz]));
- }
-
- return(FALSE);
- }
-
-
-
- /*
- * Read a grid file
- */
- grid_t *read_grid(char *fname)
- {
- int x, z;
- int fd;
- grid_t *g;
-
- if ((fd = open(fname, O_RDONLY)) == -1)
- {
- fprintf(stderr, "can't open \"%s\"\n", fname);
- perror("read");
- }
-
- g = (grid_t *)malloc(sizeof(grid_t));
-
- read(fd, &g->xsize, sizeof(int));
- read(fd, &g->zsize, sizeof(int));
-
- g->elv = (float **)malloc(sizeof(float *) * g->xsize+1);
- for (x=0; x <= g->xsize; x++)
- g->elv[x] = (float *)malloc(sizeof(float) * g->zsize+1);
-
- for (z=0; z <= g->zsize; z++)
- for (x=0; x <= g->xsize; x++)
- {
- read(fd, &g->elv[x][z], sizeof(float));
- g->elv[x][z] *= 2000.0;
- }
-
- close(fd);
- g->stepsize = 2000.0;
- g->xmin = g->zmin = 0.0;
- g->xmax = g->xsize * 2000.0;
- g->zmax = g->zsize * 2000.0;
- return(g);
- }
-
-